#include <iostream>
#include <pthread.h>
#include <cassert>
#include <string>
#include <unistd.h>

using namespace std;

#define TNUM 4
volatile bool quit = false;
typedef void (*fun_c)(const string &, pthread_cond_t *, pthread_mutex_t *);//定义一个函数指针类型
class ThreadData//创建一个类存放函数需要的参数，进行创建线程时的传参
{
public:
    ThreadData(const string &name, pthread_cond_t *pcond, pthread_mutex_t *pmtx, fun_c func)
        : name_(name),
          pcond_(pcond),
          pmtx_(pmtx),
          func_(func)
    {
    }

public:
    string name_;
    pthread_cond_t *pcond_;//条件变量指针
    pthread_mutex_t *pmtx_;//锁指针
    fun_c func_;//函数指针
};
void func1(const string &name, pthread_cond_t *pcond, pthread_mutex_t *pmtx)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        pthread_cond_wait(pcond,pmtx);//wait一定要在加锁和解锁之间执行
        cout << name << " 登陆" << endl;
        pthread_mutex_unlock(pmtx);
    }
}

void func2(const string &name, pthread_cond_t *pcond, pthread_mutex_t *pmtx)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        pthread_cond_wait(pcond,pmtx);//wait一定要在加锁和解锁之间执行
        cout << name << " 注册" << endl;
        pthread_mutex_unlock(pmtx);
    }
}
void func3(const string &name, pthread_cond_t *pcond, pthread_mutex_t *pmtx)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        pthread_cond_wait(pcond,pmtx);//wait一定要在加锁和解锁之间执行
        cout << name << " 个人信息" << endl;
        pthread_mutex_unlock(pmtx);
    }
}
void func4(const string &name, pthread_cond_t *pcond, pthread_mutex_t *pmtx)
{
    while (!quit)
    {
        pthread_mutex_lock(pmtx);
        pthread_cond_wait(pcond,pmtx);//wait一定要在加锁和解锁之间执行
        cout << name << " 退出" << endl;
        pthread_mutex_unlock(pmtx);
    }
}
void *Entry(void *args)
{
    ThreadData *td = (ThreadData *)args;
    td->func_(td->name_, td->pcond_, td->pmtx_);
}
int main()
{
    pthread_t tid[TNUM]; // 定义线程id

    pthread_mutex_t mtx; // 定义锁
    pthread_cond_t cond; // 定义条件变量

    pthread_cond_init(&cond, nullptr); // 初始化条件变量
    pthread_mutex_init(&mtx, nullptr); // 初始化创建锁

    fun_c funcs[TNUM] = {func1, func2, func3, func4}; // 定义初始化函数指针数组
    ThreadData *td;                                   // 定义传参类型
    for (int i = 0; i < TNUM; i++)                    // 创建线程
    {
        string name = "new thread";
        name += to_string(i + 1);
        td = new ThreadData(name, &cond, &mtx, funcs[i]);
        pthread_create(tid + i, nullptr, Entry, (void *)td);
    }
    sleep(3);
    int count = 10;
    while (!quit)
    {
        cout << "resume thread run code  " << count-- << endl;
        //pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond);
        if(count < 0)
        {
            quit = true;
        }
        sleep(1);
    }
    pthread_cond_broadcast(&cond);
    cout << "ctrl done" << endl;
    for (int i = 0; i < TNUM; i++) // 终止进程
    {
        pthread_join(tid[i], nullptr);
        cout << "thread " << tid[i] << "  quit " << endl;
    }

    pthread_mutex_destroy(&mtx); // 释放条件变量
    pthread_mutex_destroy(&mtx); // 释放锁

    return 0;
}